<%@ page language="java" contentType="text/html;charset=utf-8"  pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
    if (session.getAttribute("admin") == null)
        response.sendRedirect("admin_login.jsp");
%>
<html>
<head>
    <meta charset="UTF-8">
    <base href="<%=basePath%>">

    <title>My JSP 'newslist.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->

</head>

<body>
<!-- 定义table, 用于创建Easy UI的datagrid控件 -->
<div>
    <table id="orderDg1" class="easyui-datagrid"></table>
</div>
<div>
    <table id="orderDg" class="easyui-datagrid"></table>
</div>

<!-- 工具栏 -->
<div id="orderTb" style="padding:2px 5px;">
    <%--        <a href="javascript:void(0)" id="selectChoose" class="easyui-linkbutton" iconCls="icon-single-select" plain="true"--%>
    <%--           onclick="selectChoose();">单选</a>--%>
    <a href="javascript:void(0)" class="easyui-linkbutton"
       iconCls="icon-add" plain="true" onclick="editOrder();">添加权限</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove"
       onclick="removeOrder();" plain="true">删除权限</a>
</div>

<!-- 查询表单 -->
<div id="searchOrderTb" style="padding:2px 5px;">
    <form id="searchOrderForm">
        <div style="padding:3px">
            客户名称&nbsp;<input style="width:130px;" id="search_uid"
                             class="easyui-combobox" value="0" name="search_uid"
                             data-options="valueField:'id',textField:'name',url:'adminInfo/getAdmin',onChange:searchOrderInfo">&nbsp;&nbsp;&nbsp;
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="searchOrderInfo();">查找</a>
            <a href="#" class="easyui-linkbutton" iconCls="icon-clean" plain="true" onclick="resetForm('searchOrderForm')">清空</a>
        </div>
    </form>
</div>


<script type="text/javascript">
    /*设置单选和多选*/
    let selectFlag=true;
    function selectChoose() {
        selectFlag=!selectFlag;
        if(selectFlag){
            $('#orderDg').datagrid({
                singleSelect: true, //设置datagrid为单选
            })
            $('#selectChoose').linkbutton({
                iconCls: 'icon-single-select',
                text:'单选'
            });
        }else {
            $('#orderDg').datagrid({
                singleSelect: false, //设置datagrid为多选
            })
            $('#selectChoose').linkbutton({
                iconCls: 'icon-multiple-select',
                text:'多选'
            });
        }
    }

    /*清空条件查询的条件*/
    function resetForm(formId){
        $('#'+formId).form('clear');
        $('#search_uid').val('0');
        $('#search_status').val('0');
        searchOrderInfo();
    };
    $(function() {
        $('#orderDg').datagrid({
            singleSelect : true,
            url : 'orderinfo/list', //为datagrid设置数据源
            queryParams : {}, //查询条件
            pagination : true, //启用分页
            pageSize : 5, //设置初始每页记录数（页大小）
            pageList : [ 5, 10, 15 ], //设置可供选择的页大小
            rownumbers : true, //显示行号
            fit : true, //设置自适应
            toolbar : '#orderTb', //为datagrid添加工具栏
            header : '#searchOrderTb', //为datagrid标头添加搜索栏
            columns : [ [ { //编辑datagrid的列
                title : '序号',
                field : 'id',
                align : 'center',
                checkbox : true
            }, {
                field : 'userInfo',
                title : '订单客户',
                formatter : function(value, row, index) {
                    if (row.userInfo) {
                        return row.userInfo.userName;
                    } else {
                        return value;
                    }
                },
                width : 100
            }, {
                field : 'status',
                title : '订单状态',
                width : 80
            }, {
                field : 'ordertime',
                title : '订单时间',
                width : 100,
                formatter: function (value, row, index) {
                    /*调用前端自定义日期转换方法*/
                    return changeDateFormat(value)
                }
            }, {
                field : 'orderprice',
                title : '订单金额',
                width : 100
            } ] ]
        });
    });

    //转换日期格式
    function changeDateFormat(cellval) {
        if (cellval != null) {
            var date = new Date(cellval);
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            return date.getFullYear() + "-" + month + "-" + currentDate;
        }
    }

    var urls;
    var data;

    // 删除订单
    function removeOrder() {
        // 获取选中的订单记录行
        var rows = $("#orderDg").datagrid('getSelections');
        if (rows.length > 0) {
            $.messager.confirm('Confirm', '确认要删除么?', function(r) {
                if (r) {
                    var ids = "";
                    // 获取选中订单记录的订单id, 保存到ids中
                    for (var i = 0; i < rows.length; i++) {
                        ids += rows[i].id + ",";
                    }
                    // 发送请求
                    $.post('orderinfo/deleteOrder', {
                        oids : ids
                    }, function(result) {
                        if (result.success == 'true') {
                            $("#orderDg").datagrid('reload');
                        }
                        $.messager.show({
                            title: "提示信息",
                            msg: result.message,
                            style: {
                                right: '',
                                top: document.body.scrollTop + document.documentElement.scrollTop,
                                bottom: ''
                            },
                            showSpeed: 300,
                            timeout: 2000
                        });
                    }, 'json');

                }
            });
        } else {
            $.messager.alert('提示', '请选择要删除的行', 'info');
        }
    }

    // 查看明细
    function editOrder() {
        var rows = $("#orderDg").datagrid('getSelections');
        if (rows.length > 0) {
            var row = $("#orderDg").datagrid("getSelected");
            if ($('#tabs').tabs('exists', '订单明细')) {
                $('#tabs').tabs('close', '订单明细');
            }
            $('#tabs').tabs('add', {
                title : "订单明细",
                href : 'orderinfo/getOrderInfo?oid=' + row.id,
                closable : true
            });
        }else {
            $.messager.alert('提示', '请选择要修改的订单', 'info');
        }
    }

    // 查询订单
    function searchOrderInfo() {
        var oid = $('#search_oid').val();
        var status = $('#search_status').combobox("getValue");
        var uid = $('#search_uid').combobox("getValue");
        var orderTimeFrom = $("#orderTimeFrom").datebox("getValue");
        var orderTimeTo = $("#orderTimeTo").datebox("getValue");
        $('#orderDg').datagrid('load', {
            id : oid,
            status : status,
            uid : uid,
            orderTimeFrom : orderTimeFrom,
            orderTimeTo : orderTimeTo
        });
    }
</script>
</body>
</html>
